<template>
  <div class="home">
    <!-- 新增 -->
    <el-button type="primary" @click="dialogVisible = true"
      >新增</el-button
    >
    <el-dialog
      title="新增信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <div>
        <p>
          姓名: <el-input v-model="userInfo.name" placeholder="请输入内容"></el-input>
        </p>
        <p>
          住址: <el-input v-model="userInfo.address" placeholder="请输入内容"></el-input>
        </p>
        <p>
          年龄: <el-input v-model="userInfo.age" placeholder="请输入内容"></el-input>
        </p>
      </div>


      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogSuccess"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="住址" width="180">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.address }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="年龄" width="180">
        <template slot-scope="scope">
          <el-tag size="medium">{{ scope.row.age }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >

          <!-- 气泡删除 -->
          <el-popconfirm
            @cancel="cancel"
            @confirm="handleDelete(scope.row.id)"
            title="这是一段内容确定删除吗？"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  name: "Home",
  computed: {
    //  如果映射modules中定义的数据   mapState("模块名称", [...数据名称])
    ...mapState("Crud", ["tableData"]),
  },
  data() {
    return {
      dialogVisible: false,
      userInfo: {}
    };
  },
  created() {
    this.init();
  },
  methods: {
    ...mapActions("Crud", ["init", "delItem", "addItem"]),
    handleEdit(index, row) {
      console.log(index, row);
    },
    // 确定删除
    handleDelete(id) {
      this.delItem(id);
    },
    // 取消删除
    cancel() {
      this.$message({
        type: "info",
        message: "取消删除",
      });
    },
    // 新增弹框关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 新增弹框确认
    dialogSuccess() {
      // 添加id
      this.userInfo.id = String(Math.random()).slice(2, 6)
      this.addItem(this.userInfo)
      this.dialogVisible = false
    }
  },
};
</script>
